package com.box.service.search.impl;

import java.util.List;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.box.entity.solrjItem.SearchBlog;
import com.box.mapper.SolrjMapper;
import com.box.service.search.SearchBlogService;
import com.box.utils.ResponseResult;

@Service
public class SearchBlogServiceImpl implements SearchBlogService {

	@Autowired
	private SolrServer solrServer;
	
	@Autowired
	private SolrjMapper solrjMapper;
	public ResponseResult importBlog() throws Exception {
		// TODO Auto-generated method stub
		List<SearchBlog> itemList = solrjMapper.getItemList();
		for (SearchBlog blog : itemList) {		
			//创建文档对象
			SolrInputDocument document=new SolrInputDocument();
			//添加域
			document.addField("id", blog.getId());//获取id到索引库
			document.addField("blog_title", blog.getTitle());//获取标题到索引库
			document.addField("blog_summary",blog.getSummary());//摘要
			document.addField("blog_imageUrl", blog.getImageUrl());//图片
			document.addField("blog_keyword", blog.getKeyword());//关键字
			document.addField("blog_content", blog.getContent());//博客内容
			
			//写入索引库
			solrServer.add(document);
		}
		//提交
		solrServer.commit();
		return ResponseResult.ok();
	}

}
